#!/bin/bash

# Prevent the script from doing bad things
set -u  # Abort with unset variables

declare -r CURL="/usr/bin/curl"
declare -r SED="/usr/bin/sed"
declare -r IOREG="/usr/sbin/ioreg"
declare -r OPEN="/usr/bin/open"
declare -r PLISTBUDDY="/usr/libexec/PlistBuddy"
declare -r CLOVER_LAST_VERSION_URL="https://github.com/CloverHackyColor/CloverBootloader/releases"
declare -r CLOVER_APP_SUPPORT="/Library/Application Support/Clover"
declare -r CLOVER_INSTALLER_PLIST=/Library/Preferences/com.projectosx.clover.installer.plist
declare -r CLOVER_UPDATER_PLIST="$HOME"/Library/Preferences/com.projectosx.clover.updater.plist

# Argument pass to script
mode="${1:-}"

now=$(/bin/date "+%s")

# We need to check if we are allowed to update from preferences in case of
# startup
if [[ "$mode" == startup ]]; then
    # Check preferences
    scheduled_check_interval=$($PLISTBUDDY -c "Print ScheduledCheckInterval" "$CLOVER_UPDATER_PLIST" 2>/dev/null)
    [[ -z "$scheduled_check_interval" || "$scheduled_check_interval" -eq 0 ]] && exit 0
    last_check_timestamp=$($PLISTBUDDY -c "Print LastCheckTimestamp" "$CLOVER_UPDATER_PLIST" 2>/dev/null)
    [[ -z "$last_check_timestamp" ]] && last_check_timestamp=0
    [[ $(($last_check_timestamp + $scheduled_check_interval)) -gt "$now" ]] && exit 0
fi

releases=($(LC_ALL=C $CURL --silent --fail --connect-timeout 30 \
              --max-time 60 "$CLOVER_LAST_VERSION_URL" 2>&1 | \
              grep /CloverBootloader/releases/tag/ | sed -E 's/.*"([^"]+)".*/\1/'))

last_revision=$(basename ${releases[0]}) # first element in releases, last tagged revision at github


current_revision=$(LC_ALL=C $IOREG -l -pIODeviceTree | \
 $SED -nE 's@.*boot-log.*<([0-9a-fA-F]*)>.*@\1@p'    | \
 xxd -r -p                                           | \
 $SED -nE 's/^.*Clover revision: *([0-9]+).*$/\1/p')

installed_revision=$($PLISTBUDDY -c "PrintCloverRevision" "$CLOVER_INSTALLER_PLIST" 2>/dev/null)

[[ -n "$installed_revision" && "$installed_revision" -gt "$current_revision" ]] && \
 current_revision="$installed_revision"

[[ -z "$current_revision" ]] && exit 1

"$PLISTBUDDY" -c "Delete LastCheckTimestamp" "$CLOVER_UPDATER_PLIST" &>/dev/null
"$PLISTBUDDY" -c "Add LastCheckTimestamp integer $now" "$CLOVER_UPDATER_PLIST" &>/dev/null

[[ "$mode" == startup && "$last_revision" -le "$current_revision" ]] && exit 0

choice=$("$CLOVER_APP_SUPPORT"/CloverUpdater.app/Contents/MacOS/CloverUpdater \
 "$current_revision" "$last_revision")

# Remove previous key
"$PLISTBUDDY" -c "Delete AllowUpdate" "$CLOVER_UPDATER_PLIST" &>/dev/null
case "$choice" in
    -1) "$PLISTBUDDY" -c "Add AllowUpdate bool false" "$CLOVER_UPDATER_PLIST" &>/dev/null
        ;;
     1) open https://github.com/CloverHackyColor/CloverBootloader/releases/download/${last_revision}/Clover_r${last_revision}.pkg
        ;;
esac

# Local Variables:      #
# mode: ksh             #
# tab-width: 4          #
# indent-tabs-mode: nil #
# End:                  #
#
# vi: set expandtab ts=4 sw=4 sts=4: #
